import axios, { AxiosResponse } from "axios";
import Store from "@/store";
import { getUrlParams } from "@/util";

const service = axios.create({
	baseURL: import.meta.env.VITE_APP_BASE_URL,
	timeout: 10000,
});

service.interceptors.request.use(
	config => {
		const token = Store.getters["user/token"];
		token && (config.headers["token"] = token);
		return config;
	},
	error => {
		console.error("service.interceptors.request  --> ", error);
		return Promise.reject(error);
	},
);

service.interceptors.response.use(
	response => {
		const { data, config } = response,
			{ showElMsg } = getUrlParams(config.url);

		switch (data.code) {
			case 0:
				return data;
			case 1:
				JSON.parse(showElMsg) && window.$message.error(data.info);
				return Promise.reject(data);
			default:
				return data;
		}
	},
	error => {
		console.error("service.interceptors.response  --> ", error);
		return Promise.reject(error);
	},
);

export default service;
